package com.panshi.qa.provider.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.api.UserApi;
import com.panshi.qa.api.vo.QaQuestionFrontdesk;
import com.panshi.qa.provider.modules.domain.QaQuestionUncheck;
import com.panshi.qa.provider.modules.domain.ThType;
import com.panshi.qa.provider.modules.mapper.QaQuestionMapper;
import com.panshi.qa.provider.modules.mapper.QaQuestionUncheckMapper;
import com.panshi.qa.provider.modules.service.QaQuestionUncheckService;
import com.panshi.qa.provider.modules.service.ThTypeService;
import com.panshi.vo.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class QaQuestionUncheckServiceImpl extends ServiceImpl<QaQuestionUncheckMapper, QaQuestionUncheck> implements QaQuestionUncheckService {

    @Autowired
    ThTypeService thTypeService;

    @Autowired
    QaQuestionMapper qaQuestionMapper;

    @Autowired
    JavaMailSenderImpl javaMailSender;

    @Override
    public R insertQaQuestionUncheck(QaQuestionFrontdesk qaQuestionFrontdesk) {
        //查询问题标题是否存在,不存在添加,存在添加
        com.panshi.qa.provider.modules.domain.QaQuestionUncheck checkQaQuestionUncheck = this.queryTitle(qaQuestionFrontdesk.getQuestion());
        //标题已存在无法添加
        if(checkQaQuestionUncheck!=null){
            return R.error().msg("该问题标题已经存在,添加失败");
        }
        //问题未审核问题对象
        com.panshi.qa.provider.modules.domain.QaQuestionUncheck qaQuestionUncheckAdd = new com.panshi.qa.provider.modules.domain.QaQuestionUncheck();

        BeanUtils.copyProperties(qaQuestionFrontdesk,qaQuestionUncheckAdd);
        //获取未审核问题对象中的第一个img的src内容
        String img = "";
        String imgStr = null;
        boolean bool = true;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        if(!StringUtils.isEmpty(qaQuestionFrontdesk.getContent())){
            Matcher m_image = p_image.matcher(qaQuestionFrontdesk.getContent());
            while (m_image.find() && bool==true) {
                // 得到<img />数据
                img = m_image.group();
                // 匹配<img>中的src数据
                Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                while (m.find()) {
                    imgStr = m.group(1);
                    bool = false;
                    break;
                }
            }
        }
        //未审核内容中有img标签体,获取该img中src内容存储到为审核对象的图片属性中
        if(imgStr!=null){
            qaQuestionUncheckAdd.setPhotoPath(imgStr);
        }
        //添加问题对象到未审核表
        int count =  this.baseMapper.insert(qaQuestionUncheckAdd);
        //添加未审核问题对象到动态表中
        this.baseMapper.insertUserDynamic(qaQuestionUncheckAdd);
        return R.ok().msg("添加未审核问题到审核表中,添加成功....");
    }

    @Override
    public QaQuestionUncheck queryTitle(String question) {
        //根据指定标题查询该标题是否存在
        return this.baseMapper.selectOne(new QueryWrapper<QaQuestionUncheck>().eq("question",question));
    }

    @Override
    public R updateQaQuestionUncheck(com.panshi.qa.api.vo.QaQuestionUncheck qaQuestionUncheck) {
        QaQuestionUncheck qaQuestionUncheckOne = new QaQuestionUncheck();

        BeanUtils.copyProperties(qaQuestionUncheck,qaQuestionUncheckOne);

        //修改指定问题未审核表
        if(qaQuestionUncheckOne.getTypeId()==0){
            qaQuestionUncheckOne.setTypeId(13L);
        }

        int count = this.baseMapper.update(
                qaQuestionUncheckOne, new UpdateWrapper<QaQuestionUncheck>().set("status",qaQuestionUncheckOne.getStatus()).set("type_id",qaQuestionUncheckOne.getTypeId()).eq
                        ("id",qaQuestionUncheck.getId()));
        if(2 == qaQuestionUncheckOne.getStatus()){
            //修改问题未审核表的状态为2时,将该对象添加到问题审核表中
            int key = this.baseMapper.insertQuestion(qaQuestionUncheckOne);
            qaQuestionUncheckOne.setId((long) key);
            //修改问题未审核表的状态为2时,将该对象添加到动态表中
            this.baseMapper.insertUserDynamic(qaQuestionUncheckOne);
            //修改问题未审核表的状态为2时,将该对象添加到站内消息表中
            this.baseMapper.insertUserNotice(qaQuestionUncheckOne);
            //修改问题未审核表的状态为2时,将该对象添加到搜索表中
            ThType thtype = thTypeService.selectById(qaQuestionUncheckOne.getTypeId());
            qaQuestionUncheckOne.setCheckDescribe(thtype.getName());
            this.baseMapper.insertThSearch(qaQuestionUncheckOne,thtype.getName());
            try{
                //复杂邮件
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                //组装
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
                mimeMessageHelper.setSubject("857口腔项目实战");

                mimeMessageHelper.setText(mailText(key,qaQuestionUncheckOne),true);
                String email = this.baseMapper.queryUserEmail(qaQuestionUncheckOne.getUserId().intValue());
                if(!StringUtils.isEmpty(email)){
                    mimeMessageHelper.setTo(email);
                    mimeMessageHelper.setFrom("2507904063@qq.com");
                }
                javaMailSender.send(mimeMessage);
            }catch (Exception e){
                e.printStackTrace();
            }

        }

        if(count>0){
            return R.ok().msg("修改未审核通过指定问题成功");
        }
        return R.ok().msg("修改未审核通过指定问题失败");
    }

    @Override
    public R deleteUncheckQuestion(int id) {
        //根据ID删除指定的未审核表信息
        int count = this.baseMapper.deleteById(id);
        //根据ID删除指定的动态表信息
        this.baseMapper.deleteUserDynamic(id,"0");
        //删除收藏表里的指定问题
        qaQuestionMapper.deleteUserCollect(id);
        //删除搜索表的指定问题
        qaQuestionMapper.deleteThSearch(id);
        if(count>0){
            return R.ok().msg("删除未审核表指定问题成功");
        }
        return R.ok().msg("删除未审核表问题失败");
    }

    public String mailText(int key, QaQuestionUncheck qaQuestionUncheck){

        String str = "非常抱歉";
        String strhtml = "<span style=\"color: rgb(255, 0, 0);\"><strong>未通过</strong></span>";
        String value = "<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">具体描述：<span\n" +
                "\t\t\t style=\"color: rgb(255, 0, 0);\"><span style=\"color: rgb(255, 0, 0); font-size: 14px;\">"+qaQuestionUncheck.getCheckDescribe()+"</span></span></p>\n" +
                "\t\t<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">我要去重新提交，<a\n" +
                "\t\t\t href=\"http://192.168.0.118:9998/user/center/index\" target=\"_blank\" rel=\"noopener\"><strong><span style=\"color: rgb(0, 176, 240);\"></span></strong></a><strong><a\n" +
                "\t\t\t\t href=\"http://192.168.0.118:9998//user/center/index\" target=\"_blank\" style=\"font-size: 14px; white-space: normal; color: rgb(84, 141, 212);\"\n" +
                "\t\t\t\t rel=\"noopener\"><strong><span style=\"color: rgb(0, 176, 240);\">点击提交</span></strong><strong><span style=\"color: rgb(0, 176, 240);\"></span></strong></a><span\n" +
                "\t\t\t\t style=\"font-size: 14px;\"></span></strong></p>";
        if("".equals(qaQuestionUncheck.getCheckDescribe()) || "通过".indexOf(qaQuestionUncheck.getCheckDescribe())!=-1){
            str = "恭喜你";
            strhtml = "<span style=\"color: rgb(0, 176, 80);\">已通过</span></strong></p>";
            value = "\"<p style=\\\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\\\">打开看看：<strong><span\\n\" +\n" +
                    "                \"\\t\\t\\t\\t style=\\\"color: rgb(84, 141, 212);\\\"><span style=\\\"color: rgb(84, 141, 212); font-size: 14px;\\\"><a href=\\\"http://www.hasbei.com/qa/detail/202008091292297297438314496\\\"\\n\" +\n" +
                    "                \"\\t\\t\\t\\t\\t\\t rel=\\\"noopener\\\" target=\\\"_blank\\\">http://192.168.0.118:9998<wbr>/askAnswerDetail?id=<wbr>\"+key+\"</a></span></span></strong><strong><span\\n\" +\n" +
                    "                \"\\t\\t\\t\\t style=\\\"color: rgb(84, 141, 212); font-size: 14px;\\\"></span></strong><span style=\\\"color: rgb(84, 141, 212);\\\"><span\\n\" +\n" +
                    "                \"\\t\\t\\t\\t style=\\\"color: rgb(84, 141, 212); font-size: 14px;\\\"></span><br></span></p>\"";
        }
        return "<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">您好：</p>\n" +
                "\t\t<p>\n" +
                "\t\t\t<span style=\" background-color: rgb(255, 255, 255) ; ; ; ;; font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; overflow-wrap: break-word; \">\n" +
                "\t\t\t</span>\n" +
                "\t\t</p>\n" +
                "\t\t<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">\n" +
                "\t\t\t<strong>"+str+"</strong>，\n" +
                "\t\t\t您在<span style=\"font-size: 14px;\">【</span>\n" +
                "\t\t\t<a href=\"http://192.168.0.118:9998/homePage\" target=\"_blank\" style=\"color: rgb(0, 0, 0); text-decoration: underline;\" rel=\"noopener\"><span\n" +
                "\t\t\t\t style=\"color: rgb(0, 0, 0);\"><strong style=\"font-size: 14px; white-space: normal;\">857口腔项目实战</strong></span></a><span\n" +
                "\t\t\t style=\"font-size: 14px;\">】申请</span></p>\n" +
                "\t\t\t \n" +
                "\t\t<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">提问问题：<strong>【<strong>"+qaQuestionUncheck.getQuestion()+"</strong>】</strong></p><p><span style=\" background-color: rgb(255, 255, 255) ; ; ; ;; font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; overflow-wrap: break-word; \"></span></p>\n" +
                "\t\t<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">审核结果：<strong>"+strhtml+"</strong></p>"+value+"<p style=\"font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 170%; white-space: normal;\">非常感谢您对【<a\n" +
                "\t\t\t href=\"http://192.168.0.118:9998/homePage\" target=\"_blank\" style=\"text-decoration: underline; color: rgb(0, 0, 0);\" rel=\"noopener\"><span\n" +
                "\t\t\t\t style=\"color: rgb(0, 0, 0);\"><strong><span style=\"color: rgb(0, 0, 0); font-size: 14px;\">857口腔项目实战</span></strong></span></a>】\n" +
                "\t\t\t的支持。</p>";
    }
}
